package controller

import (
	"gin/common/lib"
	"gin/middleware"
	"gin/model/body"
	"gin/service/usersBodyService"
	"gin/util/jwt"
	"gin/util/map_builder"
	"gin/util/request"

	"github.com/gin-gonic/gin"
)

type UsersBodyController struct {
}

func UsersBodyRegister(router lib.RegisterRouterGroup, needLoginRouter lib.RegisterRouterGroup) {
	c := UsersBodyController{}
	needLoginRouter.POST("/usersBody/add", c.add)
	needLoginRouter.POST("/usersBody/info", c.getInfo)
	needLoginRouter.POST("/usersBody/list", c.getList)
	needLoginRouter.POST("/usersBody/record", c.getRecord)
	needLoginRouter.POST("/usersBody/detail", c.getDetail)
	needLoginRouter.POST("/usersBody/del", c.del)
}

// @Summary 添加用户参数
func (*UsersBodyController) add(c *gin.Context) {
	var p body.UsersBodyParam
	request.Bind(c, &p)
	uid := jwt.GetUid(c, true)

	usersBodyService.Add(uid, p)
	middleware.Success(c)

}

// @Summary 查询用户参数
func (*UsersBodyController) getInfo(c *gin.Context) {
	uid := jwt.GetUid(c, true)

	info := usersBodyService.GetInfo(uid)
	middleware.Success(c, info)
}

// @Summary 查询用户参数
func (*UsersBodyController) getList(c *gin.Context) {
	var p body.UsersBodySelect
	request.Bind(c, &p)

	p.Uid = jwt.GetUid(c, true)
	xAxis, yAxis := usersBodyService.GetChartList(p)
	middleware.Success(c, map_builder.BuilderMap("xAxis", xAxis, "yAxis", yAxis))
}

// @Summary 查询用户列表
func (*UsersBodyController) getRecord(c *gin.Context) {
	var p body.UsersBodyList
	request.Bind(c, &p)

	p.Uid = jwt.GetUid(c, true)
	list, count := usersBodyService.List(p)
	middleware.Success(c, map_builder.BuilderMap("list", list, "count", count))
}

// @Summary 查询用户参数详情
func (*UsersBodyController) getDetail(c *gin.Context) {
	var p body.UserBodyInfo
	request.Bind(c, &p)
	p.Uid = jwt.GetUid(c)

	info := usersBodyService.GetDetail(p)
	middleware.Success(c, map_builder.BuilderMap("info", info))
}

// @Summary 删除用户参数
func (*UsersBodyController) del(c *gin.Context) {
	var p body.UserBodyInfo
	request.Bind(c, &p)
	p.Uid = jwt.GetUid(c)

	usersBodyService.Del(p)
	middleware.Success(c)
}
